package org.voidness.squaretilesframework;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.voidness.oje2d.GLImage;
import org.voidness.oje2d.Vector2D;

/** Represents a map object */
public class MapObject {
    /** The object's image */
    private GLImage image = null;

    /** The object's filename (not the image's) */
    private String filename = null;

    /** The object's hotspot */
    private Vector2D hotSpot = null;

    /** The object's width */
    private int width;

    /** The object's height */
    private int height;

    /**
     * Default constructor
     * 
     * @param mImage The image to set
     * @param mHotSpot The hotspot position of the object
     */
    public MapObject(GLImage mImage, Vector2D mHotSpot) {
        image = mImage;
        hotSpot = mHotSpot;
    }

    /**
     * Another constructor. This one opens the sprite editor's format
     * 
     * @param mFilename The relative path to the created object
     */
    public MapObject(String mFilename) {
        try {
            filename = mFilename;
            File file = new File(filename);

            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(file);

            // Convert out input stream to a BufferedReader
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(fstream));

            // Read the hotspot
            String[] header = reader.readLine().split(" ");
            int hotspotX = Integer.parseInt(header[0]);
            int hotspotY = Integer.parseInt(header[1]);

            // Read the image size
            header = reader.readLine().split(" ");
            width = Integer.parseInt(header[0]);
            height = Integer.parseInt(header[1]);

            // Read the filename
            String filename = reader.readLine();

            // Compose the file path so it can be read by the imagePanel
            String pathToOpen =
                    file.getAbsolutePath().substring(0,
                            file.getAbsolutePath().lastIndexOf("\\"));
            // imagePanel.setImage(pathToOpen + "\\" + filename);

            // Define the new hotspot position
            // defineHotspot(new Dimension(hotspotX, hotspotY));
            hotSpot = new Vector2D(hotspotX, hotspotY);

            // Redimension the image
            // imagePanel.redimension(imageWidthSlider.getValue(),
            // imageHeightSlider.getValue());
            image =
                    new GLImage(pathToOpen + "\\" + filename, width, height,
                            false);

            // Close the input stream
            reader.close();
        } catch (NumberFormatException e) {
            // Something went wrong. Most likely, they chose a bad file.
            System.out
                    .println("This does not seem like a Freyr2D Object Description file.");
            e.printStackTrace();
        } catch (Exception e) {
            // Another weird problem happened. Print the exception.
            System.out.println("File input error.");
            e.printStackTrace();
        }
    }

    /**
     * Return's the object's image
     * 
     * @return The object's GLImage
     */
    public GLImage getImage() {
        return image;
    }

    /**
     * Returns the object's filename
     * 
     * @return A string with the object's filename (sprite editor format)
     */
    public String getFilename() {
        return filename;
    }

    /**
     * Returns the object's hotspot
     * 
     * @return The hotspot's position
     */
    public Vector2D getHotSpot() {
        return hotSpot;
    }

    /**
     * (Re)defines the object's width
     * 
     * @param mWidth The new width to set
     */
    public void setWidth(int mWidth) {
        image.setWidth(mWidth);
    }

    /**
     * (Re)defines the object's height
     * 
     * @param mHeight The new height to set
     */
    public void setHeight(int mHeight) {
        image.setHeight(mHeight);
    }

    /**
     * Draws the object using full alpha
     * 
     * @param mX The position where to draw
     * @param mY The position where to draw
     */
    public void draw(int mX, int mY) {
        draw(mX, mY, 1.0f);
    }

    /**
     * Draws the object in a given position
     * 
     * @param mX The position where to draw
     * @param mY The position where to draw
     * @param mAlpha The alpha value to use
     */
    public void draw(int mX, int mY, float mAlpha) {
        image.setWidth(width);
        image.setHeight(height);
        image.draw(mX - hotSpot.getX(), mY - hotSpot.getY(), mAlpha);

        // Draw hotspot
        // Engine.drawDot(mX, mY, 5, GLColor.RED);
    }

    /**
     * Draws the object without the hotspot
     * 
     * @param mX The position where to draw
     * @param mY The position where to draw
     * @param mAlpha The alpha value to use
     */
    public void drawNoHotSpot(int mX, int mY, float mAlpha) {
        image.draw(mX, mY, mAlpha);
    }
}